package com.LachesismStorm.lachesism.commonTools;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

import java.util.List;

public class LolMatchHistory {
    // 替换为你在拳头开发者网站获取的 API Key
    private static final String API_KEY = "RGAPI-a18713e9-6789-4d73-a9fc-d5bb9b70234c";
    private static final String SUMMONER_NAME = "等天黑";
    private static final String SUMMONER_URL = "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/";
    private static final String MATCH_LIST_URL = "https://asia.api.riotgames.com/lol/match/v5/matches/by-puuid/";
    private static final String MATCH_DETAILS_URL = "https://asia.api.riotgames.com/lol/match/v5/matches/";

    public static void main(String[] args) throws IOException {
        // 创建 OkHttp 客户端和 JSON 解析器
        OkHttpClient client = new OkHttpClient();
        ObjectMapper mapper = new ObjectMapper();

        // 第一步：获取 PUUID
        String puuid = getPuuid(client, mapper);
        System.out.println("PUUID: " + puuid);

        // 第二步：获取比赛 ID 列表
        List<String> matchIds = getMatchIds(client, mapper, puuid);
        System.out.println("比赛 ID 列表: " + matchIds);

        // 第三步：获取比赛详情（以第一场比赛为例）
        if (!matchIds.isEmpty()) {
            String matchId = matchIds.get(0);
            String matchDetails = getMatchDetails(client, mapper, matchId);
            System.out.println("比赛详情: " + matchDetails);
        }
    }

    // 获取 PUUID
    private static String getPuuid(OkHttpClient client, ObjectMapper mapper) throws IOException {
        Request request = new Request.Builder()
                .url(SUMMONER_URL + SUMMONER_NAME)
                .addHeader("X-Riot-Token", API_KEY)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                System.out.println(response);

                throw new IOException("请求失败，状态码: " + response.code());
            }
            String responseBody = response.body().string();
            JsonNode root = mapper.readTree(responseBody);
            return root.path("puuid").asText();
        }
    }

    // 获取比赛 ID 列表
    private static List<String> getMatchIds(OkHttpClient client, ObjectMapper mapper, String puuid) throws IOException {
        Request request = new Request.Builder()
                .url(MATCH_LIST_URL + puuid + "/ids")
                .addHeader("X-Riot-Token", API_KEY)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败，状态码: " + response.code());
            }
            String responseBody = response.body().string();
            return mapper.readValue(responseBody, List.class);
        }
    }

    // 获取比赛详情
    private static String getMatchDetails(OkHttpClient client, ObjectMapper mapper, String matchId) throws IOException {
        Request request = new Request.Builder()
                .url(MATCH_DETAILS_URL + matchId)
                .addHeader("X-Riot-Token", API_KEY)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败，状态码: " + response.code());
            }
            return response.body().string();
        }
    }
}